<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class sales_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
		

	

	public function get_purchaserList(){
		$this->db->select('users.*,branch.name AS branch_name');
		$this->db->from('users', 'branch','users_groups' );
		$this->db->join('branch', 'users.company= branch.id');
		$this->db->join('users_groups', 'users.id= users_groups.user_id AND users_groups.group_id= 3  '  );
		//$this->db->join('company', 'product.id_company= company.id');
		$result = $this->db->get();
		return $result;
	} 

public function get_product($code,$id_branch){
	$query = $this->db->get_where('product', array('code' => $code));
	$product = $query->row_array();
	$n = $query->num_rows();
	if($n > 0){
		$query1 = $this->db->get_where('branch_product', array('id_product' => $product['id'],'id_branch' => $id_branch));		
		$n1 = $query1->num_rows();
		if($n1 > 0){
			$product = $query->row_array();
			echo $product['selling_price']; 
		}else{
			
			echo 2;
		}
		
	}else{
		
		echo 1;
		}
	}	
	
public function  get_invoiceid(){
	$this->db->select_max('id');
$query = $this->db->get('sales');
	return $query; 
		
}	

public function get_invoicecode($id_branch){
	
	$query = $this->db->get_where('branch', array('id' => $id_branch));	
	return $query;
}


	
	public function get_tax(){
	$q = $this->db->get('tax');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
		
	}	
	
public function  get_customer(){
		$this->db->select('users.*');
	$this->db->from('users', 'users_groups' );
	$this->db->join('users_groups', 'users.id= users_groups.user_id AND users_groups.group_id= 4  '  );
	//$this->db->join('company', 'product.id_company= company.id');
	$q = $this->db->get();
		//$q = $this->db->get_where('customer',array(''=>'');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
	}	

	

public function insert($data){
		if($data['transaction_type']==0){
			$data['transaction_type']="Cash";
			$this->db->insert('sales',$data);
			$latest_id = $this->db->insert_id();
			/*if($data['customer_name']!=''){
				$data['customer_name']= ' ';	
			}
			*/$data1 = array(
			'id_user' => $data['id_customer'],
			'user_name' => $data['customer_name'],
			'voucher_type' => 'sales',
			'payment_type' => 0,
			'payment_mode' => 'Cash',
			'voucher_no' => $data['invoice_no'],
			'date' => $data['date'],
			'debit_amount' => $data['total_price'],
			'credit_amount' => $data['total_price'],
			'ip' => $_SERVER['REMOTE_ADDR']
			);
			//print_r($data1);exit;
		$this->db->insert('accounts',$data1);
		}else{
			$data['transaction_type']="Credit";
			$this->db->insert('sales',$data);
			$latest_id = $this->db->insert_id();
			$data1 = array(
			'id_user' => $data['id_customer'],
			'user_name' => $data['customer_name'],
			'voucher_type' => 'sales',
			'payment_type' => 0,
			'payment_mode' => '',
			'voucher_no' => $data['invoice_no'],
			'date' => $data['date'],
			'debit_amount' => $data['total_price'],
			'ip' => $_SERVER['REMOTE_ADDR']
			);
			//print_r($data1);exit;
		$this->db->insert('accounts',$data1);
		}
			return $latest_id;
	}



public function insert_item($data,$id_branch){
		$product = $this->db->get_where('product', array('id' => $data['id_product']))->row_array();	
		$branch_product = $this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->row_array();		
			$new_qty1 = $branch_product['opening_stock'] - $data['quantity'];
			$data2 = array(
				'opening_stock' => $new_qty1 
			);	
			$this->db->update('branch_product',$data2,array('id'=>$branch_product['id']));
			$new_qty = $product['opening_stock'] - $data['quantity'];
			$data3 = array(
			'opening_stock' => $new_qty 
			);	
			
		$this->db->update('product',$data3,array('id'=>$data['id_product']));
		$this->db->insert('sales_product',$data);
		
	}	





public function get_profile(){
		
	$query = $this->db->get('company_profile');

	return $query;
}	


/*public function get_groups(){
	$this->db->select('acc_group.*,projects.name');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$id);
		$this->db->join('projects','tasks.project_id=projects.id');
		return $this->db->get()->row_array();
		$query = $this->db->get('acc_group');
	return $query;
}
*/

public function get_invoiceno(){
	
	$this->db->from('sales');
	$this->db->order_by("id", "asc");
	$this->db->limit(1);
	$query = $this->db->get(); 
	return $query;

}


public function search_quantity($code,$qty, $id_branch){
	$query1 = $this->db->get_where('product', array('code' => $code));
	$product = $query1->row_array();
	$query = $this->db->get_where('branch_product', array('id_product' => $product['id'],'id_branch' => $id_branch));
	$branch_product = $query->row_array();
	if(($branch_product['opening_stock']>= $qty) && ($product['opening_stock']>=$qty)){
		echo 1;	
	}else if($branch_product['opening_stock']< $qty && $product['opening_stock']>=$qty){
		
		echo 2;
	}else{
		
		echo 3;
	}
	
	
}


	public function get_salesList(){
		$this->db->select('sales.*, users.username as customer_name');
		$this->db->from('sales','users' );
		$this->db->join('users', 'sales.id_customer= users.id'  );
		$result = $this->db->get();
		return $result;
	} 

	
public function get_uniquesale($id){
	$query = $this->db->get_where('sales_product', array('id_sale' => $id));
	return $query;
	
}	

public function delete($id){
		$this->db->delete('sales',array('id'=>$id));
		$this->db->delete('sales_product',array('id_sale'=>$id));		
	}

public function get_sales($id){
	
	$query = $this->db->get_where('sales', array('id' => $id));
	return $query;
}

public function get_customer_details($id){
	$query = $this->db->get_where('users', array('id' => $id));
	return $query;	
}

public function get_company_profile(){
	
	$query = $this->db->get('company_profile');
	return $query;
}

public function getID($code){	
	$query = $this->db->get_where('product', array('code' => $code))->row_array();
	return $query['id'];
}

public function get(){
	   $this->datatables
		->select('sales.id as id, sales.invoice_no,sales.invoice_type,sales.date,sales.customer_name as customer_name,sales.amount, sales.total_disc,sales.total_excise, sales.total_tax,sales.total_price,sales.transaction_type')
		->from('sales' )
		/*	
			->select('product.id as id ,product.name,product.code, category.name AS category_name, company.name AS company_name,product.purchase_price,product.selling_price,product.opening_stock,product.alert_quantity')
			->from('product', 'company', 'category')
			->join('category', 'product.id_category= category.id')
			->join('company', 'product.id_company= company.id')*/
			->add_column("Actions", 
			"<center>			<a href='report/$1'>
								<i class='icon-print'></i>
								</a>
							   </center>", "id")
			->unset_column('id');
		
		
	   return  $this->datatables->generate();
	
}

function get_cname($id){
		$query = $this->db->get_where('users', array('id' => $id))->row_array();
		if(count($query)>0){
				return $query['username'];
		}else{
			return '--';
		}
	
	
}


function getby_code($code,$name,$category){
			$where = "product.code  LIKE '$code%' ";
			if($name!=''){
			$where.= "AND product.name  LIKE '$name%' ";
			}
			if($category!=''){
			$where.= "AND category.name  LIKE '$category%' ";
			}
	
				$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
			$this->db->where($where);
			$all_sales=$this->db->get();
				return $all_sales;



}
function getby_name($name,$code, $category){
	
			$where = "product.name LIKE '$name%' ";
			if($code!=''){
			$where.= "AND product.code LIKE '$code%' ";
			}
			if($category!=''){
			$where.= "AND category.name  LIKE '$category%' ";
			}
	
		$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
			$this->db->where($where);
			$all_sales=$this->db->get();
				return $all_sales;

	
}
function getby_category($category, $code, $name){
		
			$where = "category.name LIKE '$category%' ";
			if($code!=''){
			$where.= "AND product.code LIKE '$code%' ";
			}
			if($name!=''){
			$where.= "AND product.name  LIKE '$name%' ";
			}

		$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
			$this->db->where($where);
			$all_sales=$this->db->get();
			return $all_sales;


	
}


}
